// enum like for pages names
var Pages = {
	HOME : "home",
	VIEWITEM : "viewItem",
	REGISTER : "register",
	SEARCH : "search",
	POSTITEM : "postItem"
};
// stores the current page name
var currentPage = Pages.HOME;
var nestedPage;

function isHomePage() {
	return currentPage == Pages.HOME;
}

function isSearchPage() {
	return currentPage == Pages.SEARCH;
}

function isViewItemPage() {
	return currentPage == Pages.VIEWITEM;
}

function switchToRegister() {
	extendPage(500);
	getNestedPage().src = "register.html";
	currentPage = Pages.REGISTER;
	removeHome();
}

function switchToPostItem() {
	extendPage(1000);
	getNestedPage().src = "newitem.html";
	currentPage = Pages.POSTITEM;
	removeHome();
}

function switchToViewItem() {
	getNestedPage().src = "itemview.html";
	extendPage(1000);
	currentPage = Pages.VIEWITEM;
	removeHome();
}

function switchToSearch() {
	returnToHome();
	currentPage = Pages.SEARCH;
    emptyContainer(getItemsTableBody());
	emptyContainer(getBidsTableBody());
	removeBidsTable();
}

function getNestedPage() {
	if (nestedPage == null)
		nestedPage = document.getElementById("nestedpage");
	return nestedPage;
}

function extendPage(height) {
	getNestedPage().style.height = height + "px";
	getNestedPage().style.width = "100%";
}

function collapsePage() {
	getNestedPage().style.height = "0px";
	getNestedPage().style.width = "0px";
}
// stores the HTML frame of the items table
var itemsTable, bidsTable;

function removeHome() {
	emptyContainer(getItemsTableBody());
	removeItemsTable();
	emptyContainer(getBidsTableBody());
	removeBidsTable();
	emptyContainer(document.getElementById("statistics"));
	stopRefreshingBids();
	stopRefreshingStatistics();
}

function returnToHome() {
	if (isHomePage())
		return;
	if (bidsTable != null)
		document.body.appendChild(bidsTable);
	refetchNewBidsFromCache();
	emptyCachedItems();
	startRefreshingBids();
	startRefreshingStatistics();
	if (isSearchPage()) {
		emptyContainer(getItemsTableBody());
		currentPage = Pages.HOME;
		refetchNewItemsFromCache();
		return;
	}
	currentPage = Pages.HOME;
	collapsePage();
	getNestedPage().removeAttribute("src");
	document.body.appendChild(itemsTable);
	refetchNewItemsFromCache();

}

function getItemsTableBody() {
	return document.getElementById("itemtsablebody");
}

function getItemsContainer() {
	return document.getElementById("items");
}

function getBidsTableBody() {
	return document.getElementById("bidstsablebody"); 
}

function getBidsContainer(){
	return document.getElementById("bids"); 
}

function emptyContainer(container) {
	if (container != null)
		container.innerHTML = "";
}

function removeItemsTable() {
	 var items = getItemsContainer();
	if (items != null) {
		itemsTable = items;
		document.body.removeChild(itemsTable);
	}
}

function removeBidsTable() {
	 var bids = getBidsContainer();
	 if (bids != null) {
		bidsTable = bids;
		document.body.removeChild(bidsTable);
	}
}